package com.sinaapp.gavinzhang.GSpider.common;

import com.sinaapp.gavinzhang.GSpider.config.Constants;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author gavin
 * @version 1.0
 * @date 2016/4/5
 * @description 读取文件
 */
public class FileUtil {
    public static byte[] readBytes(InputStream in) throws IOException {
        BufferedInputStream bufin = new BufferedInputStream(in);
        int buffSize = Constants.BUFFSIZE;
        ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
        byte[] temp = new byte[buffSize];
        int size = -1;
        while ((size = bufin.read(temp)) != -1) {
            out.write(temp, 0, size);
            if (size!=1024)break;
        }
        bufin.close();
        return out.toByteArray();
    }

    public static String readBytes(InputStream in,String charset)throws IOException{
        byte[] byteArray = FileUtil.readBytes(in);
        return new String(byteArray,charset);
    }
}
